//
//  UserControllrt.m
//  TribleWar
//
//  Created by ＣＳＴ ＣＳＴ on 8/13/12.
//  Copyright (c) 2012 ＣＳＴ. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "RegistViewController.h"
#import "GameACK.pb.h"
#import "ViewController.h"
#import "GameMove.pb.h"
#import "Singleton.h"
@interface UserController : GHTestCase
{
    RegistViewController *registController;
    ViewController *viewController;
}
@end

@implementation UserController

- (void)setUp
{
    registController = [[RegistViewController alloc]init];
    viewController = [[ViewController alloc]init];

}

- (void)tearDown
{
    registController = nil;
    viewController = nil;
}

- (void)testRegex
{
    NSString * regex = @"(^[A-Za-z0-9]{6,15}$)";  
    NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];  
    GHAssertTrue([pred evaluateWithObject:@"123456ABCde"], @"the expression is illegal");
    GHAssertTrue([pred evaluateWithObject:@"123esf"], @"the expression is illegal");
    GHAssertFalse([pred evaluateWithObject:@"123eghgjhguguiguiguuudsd"], @"the expression is illegal");
}

- (void)testRegistFailWithIllegalAccount
{
    NSString *account = @"";
    NSString *pwd = @"123456";
    NSString *repwd = @"123456";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], @"regist must failed when account unless than 3 and less than 10");

    account = @"12345678912";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], @"regist must failed when account unless than 3 and less than 10");
    
    account = @"12";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], @"regist must failed when account unless than 3 and less than 10");
    
    account = @"123!@#$";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], @"regist must failed when account illegal");
    
    account = @"123456";
    pwd = @"123456";
    repwd = @"ssssss";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], @"regist must failed when pwd unless than 6");
    
    account = @"123456";
    pwd = @"123456";
    repwd = @"";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], @"regist must failed when pwd unless than 6");
    
    
    account = @"123!@#$";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], @"regist must failed when account illegal");
}

- (void)testRegistFailWithIllegalPwd
{
    NSString *account = @"123456";
    NSString *pwd = @"";
    NSString *repwd = @"123456";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], pwd);
    GHAssertEqualStrings([registController connectStatus], @"密码必须大于6位", @"密码必须大于6位");
    pwd = @"123";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], pwd);
    
    pwd = @"123456";
    repwd = @"";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], repwd);
    GHAssertEqualStrings([registController connectStatus], @"密码必须大于6位", @"密码必须大于6位");

    pwd = @"123";
    repwd = @"123456";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], repwd);
    GHAssertEqualStrings([registController connectStatus], @"密码必须大于6位", @"密码必须大于6位");

    pwd = @"123456";
    repwd = @"1234567";
    GHAssertFalse([registController isValid:account password:pwd repassword:repwd], pwd, repwd);
    GHAssertEqualStrings([registController connectStatus], @"两次密码输入不一致", @"两次密码输入不一致");

    
}

-(void)testRegistSuccess
{
    NSString *account = @"123456";
    NSString *pwd = @"123456";
    NSString *repwd = @"123456";
    GHAssertTrue([registController isValid:account password:pwd repassword:repwd], account, pwd, repwd);
}

- (void)testLoginFailed
{
    NSString *account = @"";
    NSString *pwd = @"";
    GHAssertFalse([viewController isValid:account password:pwd] , @"empty account password");
    
    account = @"12";
    pwd = @"123456";
    GHAssertFalse([viewController isValid:account password:pwd] , account);

    account = @"123456";
    pwd = @"12345";
    GHAssertFalse([viewController isValid:account password:pwd] , pwd);
    
    account = @"123@#!";
    pwd = @"123456";
    GHAssertFalse([viewController isValid:account password:pwd] , account);

}

- (void)testLoginSuccess
{
    NSString *account = @"123456";
    NSString *pwd = @"123456";

    GHAssertTrue([viewController isValid:account password:pwd] , account);
    
}


-(void)testLoginCallback
{
    NSString *loginFail = @"服务器异常，登录失败";
    Ack *ack;
    [viewController loginCallback:(Ack *)ack];
    GHAssertEqualStrings(viewController->loginStatus, loginFail, @"ack is nil"); 

    ack = [[Ack builder]build];
    [viewController loginCallback:ack];
    GHAssertEqualStrings(viewController->loginStatus, loginFail, @"msg empty");

    ack = [[[[Ack builder]setSuccess:NO]setMsg:@"" ]build];
    [viewController loginCallback:ack];
    GHAssertEqualStrings(viewController->loginStatus, loginFail, @"error with ack");
    
    ack = [[[Ack builder]setSuccess:YES]build];
    [viewController loginCallback:(Ack *)ack];
    GHAssertEqualStrings(viewController->loginStatus, @"登录成功", @"user success");

    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    long long dTime = [[NSNumber numberWithDouble:time] longLongValue]; // 将double转为long long型
    NSString *curTime = [NSString stringWithFormat:@"%llu",dTime]; // 输出long long型
    NSLog(@"Currtime - %@", curTime);
}

- (void)testInstance
{
    Ack *ack1;
    GHAssertNil(ack1, @"ack is nil");
    
    Ack *ack2 = [[Ack builder]build];
    GHAssertNotNil(ack2, @"obj is nil");
    GHAssertEqualStrings([ack2 msg], @"", @"msg nil");
    Ack *ack3 = [[[Ack builder]setMsg:@"11"]build];
    GHAssertNotNil(ack3, @"");
    GHAssertEqualStrings([ack3 msg], @"11",  @"msg == 11");
    GHAssertFalse([ack3 success], @"success false");
    
    
    Move *move1;
    GHAssertNil(move1.state, @"move is nil");
    GHAssertNil(move1.state.curPos, @"");
    GHAssertEquals(1.0, 1.0, @"sssssss");

    NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
    int curTime = [[NSNumber numberWithDouble: timeStamp]intValue];
    
    Vector *curPos = [[[[Vector builder]setX:9]setY:5]build];
    Vector* purPos = [[[[Vector builder]setX:9]setY:5]build];
    State* state = [[[[[State builder]setCurPos:curPos]setPurPos:purPos]setCurTime:curTime]build];
    Move* move2= [[[[[Move builder]setCategory:CategoryProp ]setState:state]setUid:0]build];
    GHAssertEquals(move2.category, CategoryProp, @"sss");
    GHAssertNil(move1.state, @"sssss");
    
}

-(void)testRegistCallback
{
    Ack *ack;
    NSString *registFail = @"服务器异常，注册失败";
    [registController registCallback:(Ack *)ack];
    GHAssertEqualStrings([registController connectStatus], registFail, @"ack is nil"); 
    
    ack = [[Ack builder]build];
    [registController registCallback:ack];
    GHAssertEqualStrings([registController connectStatus], registFail, @"msg empty");
    
    ack = [[[[Ack builder]setSuccess:NO]setMsg:@"" ]build];
    [registController registCallback:ack];
    GHAssertEqualStrings([registController connectStatus], registFail, @"error with ack");
    
    
    ack = [[[[Ack builder]setSuccess:NO]setMsg:@"用户名已存在"]build];
    [registController registCallback:(Ack *)ack];
    GHAssertEqualStrings([registController connectStatus], @"用户名已存在", @"user had exsit");
    
    ack = [[[Ack builder]setSuccess:YES]build];
    [registController registCallback:(Ack *)ack];
    GHAssertEqualStrings([registController connectStatus], @"注册成功", @"user success");
    
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    long long dTime = [[NSNumber numberWithDouble:time] longLongValue]; // 将double转为long long型
    NSString *curTime = [NSString stringWithFormat:@"%llu",dTime]; // 输出long long型
    NSLog(@"Currtime - %@", curTime);
}


- (void)testStringEmpty
{
    NSString *ss;

    GHAssertNil(ss , @"ss is nil");
    
    GHAssertTrue([ss length] == 0, @"length is 0");
}

- (void)testSingleton
{
    [[Singleton shareSingle] sayHello];
    Singleton *ss = [[Singleton alloc]init];
    GHAssertNil(ss , @"ss is nil");
}
@end
